package com.ywq.app.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.ywq.app.biz.AdminBiz;
import com.ywq.app.biz.ArtMadeBiz;
import com.ywq.app.biz.IdentifierBiz;
import com.ywq.app.biz.MailBiz;
import com.ywq.app.biz.MemberBiz;
import com.ywq.app.biz.impl.mail.CcMailBizImpl;
import com.ywq.app.dao.ArtistDao;
import com.ywq.app.domain.AdminUser;
import com.ywq.app.domain.ArtArtist;
import com.ywq.app.domain.ArtArtistPic;
import com.ywq.app.domain.ArtMade;
import com.ywq.app.domain.ArtMadeAudit;
import com.ywq.app.domain.ArtMadeTalk;
import com.ywq.app.domain.ArtPic;
import com.ywq.app.domain.ArtRoom;
import com.ywq.app.domain.ArtRoomArtist;
import com.ywq.app.domain.Member;
import com.ywq.app.domain.MemberAddress;
import com.ywq.app.domain.MemberHas;
import com.ywq.common.constant.PageSize;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;
import com.ywq.common.util.IdGenerator;

public class ArtMadeBizImpl  implements ArtMadeBiz{
	private final static Logger logger = LoggerFactory.getLogger(ArtMadeBizImpl.class);
	@Autowired
	private AdminBiz adminBiz;
	@Autowired
	private MemberBiz memberBiz;
	@Autowired
	private MailBiz mailBiz;
	@Autowired
	private IdentifierBiz idBiz;
	
	private ArtistDao artistDao;
	public void setArtistDao(ArtistDao artistDao) {
		this.artistDao = artistDao;
	}
	//定制的艺术家
	@Override
	public PageModel getArtistList(Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_ADMIN);
		map.put("pageSize", PageSize.PAGE_SIZE_ADMIN);
		map.put("type",PageSize.ARTIST_MADE);
		int count = artistDao.getArtistListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artistDao.getArtistList(map));
		pm.setPageSize(PageSize.PAGE_SIZE_ADMIN);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_ADMIN - 1)/ PageSize.PAGE_SIZE_ADMIN);
		return pm;
	}
	//增加艺术家
	@Transactional(rollbackForClassName = {"com.ywq.common.exception.AppBusinessException;"}, timeout = 10)
	@Override
	public void insertArtArtist(ArtArtist artArtist) throws AppBusinessException {
			//String istId = IdGenerator.createIdentifier();
			String istId = idBiz.generateId("art_artist");
			artArtist.setId(istId);
			artArtist.setType(PageSize.ARTIST_MADE);
			//增加画家登陆账号(定制)
			AdminUser user =new AdminUser();
			user.setArtistId(istId);
			user.setUsername(artArtist.getUsername());
			user.setPassword("111111");
			user.setRealname(artArtist.getName());
			user.setDescription(artArtist.getDetail());
			user.setEmail(artArtist.getEmail());
			adminBiz.insertUser(user);
			artistDao.insertArtArtist(artArtist);
			Member member =new Member();
			member.setEmail(artArtist.getEmail());
			mailBiz.sendEmail("",  member, "一封来自笔飞的邮件", "您的登陆账号为："+user.getUsername()+"密码是：111111");
		}
	//艺术家的作品列表
	@Override
	public PageModel getArtistPicList(Integer page,String artistId) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_ADMIN);
		map.put("pageSize", PageSize.PAGE_SIZE_ADMIN);
		map.put("artistId",artistId);
		int count = artistDao.getArtistPicListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artistDao.getArtistPicList(map));
		pm.setPageSize(PageSize.PAGE_SIZE_ADMIN);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_ADMIN - 1)/ PageSize.PAGE_SIZE_ADMIN);
		return pm;
	}
	
	@Override
	public PageModel getMadeArtistList(Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_MADE);
		map.put("pageSize", PageSize.PAGE_SIZE_MADE);
		map.put("type",PageSize.ARTIST_MADE);
		int count = artistDao.getArtistListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artistDao.getMadeArtistList(map));
		pm.setPageSize(PageSize.PAGE_SIZE_MADE);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_MADE - 1)/ PageSize.PAGE_SIZE_MADE);
		return pm;
	}
	@Override
	public PageModel getMadeArtistCsList(Integer page,String classId) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_MADE);
		map.put("pageSize", PageSize.PAGE_SIZE_MADE);
		map.put("type",PageSize.ARTIST_MADE);
		map.put("artClass",classId);
		int count = artistDao.getArtistListCsCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtArtist> artArtistList =artistDao.getMadeArtistCsList(map);
		//
		if(artArtistList.size()>0){
			for(ArtArtist artArtist:artArtistList){
				Map<String, Object> mapFour = new HashMap<String, Object>();
				mapFour.put("artClass",classId);
				mapFour.put("artistId",artArtist.getId());
				List<ArtPic> artPicList =artistDao.getFourPicMadeByClass(mapFour);
				artArtist.setArtPic(artPicList);
			}
		}
		pm.setList(artArtistList);
		pm.setPageSize(PageSize.PAGE_SIZE_MADE);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_MADE - 1)/ PageSize.PAGE_SIZE_MADE);
		return pm;
	}
	//展示页面艺术家列表页
	@Override
	public PageModel getMadeNiceArtistList(Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_MADE_NICE);
		map.put("pageSize", PageSize.PAGE_SIZE_MADE_NICE);
		map.put("type",PageSize.ARTIST_MADE);
		int count = artistDao.getArtistListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artistDao.getMadeArtistList(map));
		pm.setPageSize(PageSize.PAGE_SIZE_MADE_NICE);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_MADE_NICE - 1)/ PageSize.PAGE_SIZE_MADE_NICE);
		return pm;
	}
	//查询作品根据艺术家标识
	@Override
	public List<ArtArtistPic> getPicListByIstId(String id) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("artistId",id);
		List<ArtArtistPic> picList =artistDao.getArtistPicList(map);
		return picList;
	}
	//更改艺术家
	@Override
	public void updateArtist(ArtArtist artArtist) throws AppBusinessException {
			artistDao.updateArtist(artArtist);
		}
	//定制的艺术家
		@Override
		public PageModel getArtistListForMisc(Integer page,Integer pageSize) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
			map.put("type",PageSize.ARTIST_MADE);
			int count = artistDao.getArtistListCount(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getArtistList(map));
			pm.setPageSize(pageSize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pageSize - 1)/ pageSize);
			return pm;
		}
		@Override
		public PageModel getArtistPicListForMisc(Integer page,String artistId,Integer pageSize) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
			map.put("artistId",artistId);
			int count = artistDao.getArtistPicListCount(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getArtistPicList(map));
			pm.setPageSize(pageSize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pageSize - 1)/ pageSize);
			return pm;
		}
		
		//艺术家页面专用
		@Override
		public PageModel getArtistPicListForArtist(Integer page,String artistId,Integer pageSize,String classId) throws AppBusinessException {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
			map.put("artistId",artistId);
			map.put("classId",classId);
			int count = artistDao.getArtistPicListCount(map);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getArtistPicList(map));
			pm.setPageSize(pageSize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pageSize - 1)/ pageSize);
			return pm;
		}
		/**
		 * 艺术家详情
		 */
		@Override
		public  ArtArtist getArtist(String id) throws AppBusinessException{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", id);
			return artistDao.getArtist(map);
		}
		/**
		 * 增加定制订单
		 */
		@Transactional(rollbackForClassName = {"com.ywq.common.exception.AppBusinessException;"}, timeout = 10)
		@Override
		public void insterMade(ArtMade artMade ) throws AppBusinessException {
			//String id = IdGenerator.createIdentifier();
			String id = idBiz.generateId("art_made");
			artMade.setIdentifier(id);
			artistDao.insterMade(artMade);
			//发送邮件
			CcMailBizImpl send =CcMailBizImpl.getInstance(); 
			//to artistId
			Member memebr =memberBiz.getMemberById(artMade.getMemberId());
			AdminUser adminUser =artistDao.getUserByArtistId(artMade.getArtistId());
			if(!"".equals(adminUser.getEmail())){
				String to[]={adminUser.getEmail()};
				String cs[] = {memebr.getEmail(),"befamousstore@163.com"};
				String ms[] = null;
				String subject=adminUser.getUsername()+",您有定制信息，请尽快查看。"; 
				String content="您好，您有一份新的定制订单，请您登陆http://befamousstore.com，定制单号为："+id; 
				String formEmail="befamousstore@163.com";
				send.send(to,cs,ms,subject,content,formEmail); 
			}
			
		}
		/**
		 * 根据艺术家id查询定制订单
		 * 
		 * 
		 */
		@Override
		public PageModel getMadeOrderList(Map<String,Object> map) throws AppBusinessException {
			int page =(Integer) map.get("page");
			int pagesize =(Integer) map.get("pagesize");
			String artistId="";
			String memberId="";
			if(!"".equals((String) map.get("artistId")) && (String) map.get("artistId")!=null)
			{
				artistId =(String) map.get("artistId");
			}
			if(!"".equals((String) map.get("memberId")) && (String) map.get("memberId")!=null)
			{
				memberId =(String) map.get("memberId");
			}
			Map<String, Object> mapOrder= new HashMap<String, Object>();
			mapOrder.put("startId", (page - 1) * pagesize);
			mapOrder.put("pagesize", pagesize);
			mapOrder.put("artistId",artistId);
			mapOrder.put("memberId",memberId);
			int count = artistDao.getMadeOrderListCount(mapOrder);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getMadeOrderList(mapOrder));
			pm.setPageSize(pagesize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pagesize - 1)/ pagesize);
			return pm;
		}
		/**
		 * 对话列表  根据定制单号
		 */
		@Override
		public List<ArtMadeTalk> getTalkListByMadeId(String madeId,String madeStage)throws AppBusinessException {
			Map<String, Object> mapTalk= new HashMap<String, Object>();
			mapTalk.put("madeStage", madeStage);
			mapTalk.put("madeId", madeId);
			return artistDao.getTalkListByMadeId(mapTalk);
		}
		//某一订单所有对话
		@Override
		public PageModel getTalkGrid(Map<String,Object> map) throws AppBusinessException {
			int page =(Integer) map.get("page");
			int pagesize =(Integer) map.get("pagesize");
			String madeId=(String) map.get("madeId");
			
			Map<String, Object> mapOrder= new HashMap<String, Object>();
			mapOrder.put("startId", (page - 1) * pagesize);
			mapOrder.put("pagesize", pagesize);
			mapOrder.put("madeId", madeId);
			int count = artistDao.getTalkGridCount(mapOrder);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			List<ArtMadeTalk> amtList=artistDao.getTalkGrid(mapOrder);
			if(amtList.size()>0){
				for(ArtMadeTalk artMadeTalk :amtList){
					if("artist".equals(artMadeTalk.getMemberType())){
						AdminUser adminUser =artistDao.getUserByArtistId(artMadeTalk.getMemberId());
						artMadeTalk.setMemberName(adminUser.getUsername());
					}else if("member".equals(artMadeTalk.getMemberType())){
						Member member =memberBiz.getMemberById(artMadeTalk.getMemberId());
						artMadeTalk.setMemberName(member.getNickname());
					}
				}
			}
			pm.setList(amtList);
			pm.setPageSize(pagesize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pagesize - 1)/ pagesize);
			return pm;
		}
		
		/**
		 * 定制详情
		 */
		@Override
		public  ArtMade getArtMadeById(String id) throws AppBusinessException{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("identifier", id);
			return artistDao.getArtMadeById(map);
		}
		//定制付款订单
		@Override
		public  ArtMadeAudit getArtMadeAuditById(String id) throws AppBusinessException{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("auditId", id);
			return artistDao.getArtMadeAuditById(map);
		}
		//
		@Override
		public  ArtMadeAudit getArtMadeAuditByMadeId(String madeId,String auditKey) throws AppBusinessException{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("madeId", madeId);
			map.put("auditKey", auditKey);
			return artistDao.getArtMadeAuditById(map);
		}
		
		//新增talk
		@Override
		public void insertArtMadeTalk(ArtMadeTalk artMadeTalk ) throws AppBusinessException {
			//String id = IdGenerator.createIdentifier();
			String id = idBiz.generateId("art_made_talk");
			artMadeTalk.setId(id);
			artistDao.insertArtMadeTalk(artMadeTalk);
		}
		//新增审核
		@Transactional(rollbackForClassName = {"com.ywq.common.exception.AppBusinessException;"}, timeout = 10)
		@Override
		public void insertMadeAudit(ArtMadeAudit artMadeAudit ) throws AppBusinessException {
			String id = idBiz.generateId("art_made_audit");
			artMadeAudit.setId(id);
			artistDao.insertMadeAudit(artMadeAudit);
			//发送邮件
			CcMailBizImpl send =CcMailBizImpl.getInstance(); 
			//to artistId
			AdminUser adminUser =artistDao.getUserByArtistId(artMadeAudit.getArtistId());
			//ArtMade artMade =getArtMadeById(artMadeAudit.getMadeId());
			//Member memebr =memberBiz.getMemberById(artMade.getMemberId());
			if(!"".equals(adminUser.getEmail())){
				String to[]={"befamousstore@163.com"};
				String cs[] = {adminUser.getEmail()};
				String ms[] = null;
				String subject="艺术家："+adminUser.getUsername()+"已经提交了定制审核,，请您尽快查看并审核。"; 
				String content="请您登陆http://befamousstore.com，审核单号为："+id; 
				String formEmail="befamousstore@163.com";
				send.send(to,cs,ms,subject,content,formEmail); 
			}
		}
		//所有审批
		@Override
		public PageModel getArtMadeAuditGrid(Integer page,Integer pagesize) throws AppBusinessException {
			Map<String, Object> mapOrder= new HashMap<String, Object>();
			mapOrder.put("startId", (page - 1) * pagesize);
			mapOrder.put("pagesize", pagesize);
			int count = artistDao.getMadeAuditCount(mapOrder);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getArtMadeAuditGrid(mapOrder));
			pm.setPageSize(pagesize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pagesize - 1)/ pagesize);
			return pm;
		}
		//艺术家审批
		@Override
		public PageModel queryArtistAuditListGrid(String artistId,Integer page,Integer pagesize) throws AppBusinessException {
			Map<String, Object> mapOrder= new HashMap<String, Object>();
			mapOrder.put("startId", (page - 1) * pagesize);
			mapOrder.put("pagesize", pagesize);
			mapOrder.put("artistId", artistId);
			int count = artistDao.getMadeAuditCount(mapOrder);
			PageModel pm = new PageModel();
			pm.setCurrentPage(page);
			pm.setList(artistDao.getArtMadeAuditGrid(mapOrder));
			pm.setPageSize(pagesize);
			pm.setTotalRecord(count);
			pm.setTotalPage((count + pagesize - 1)/ pagesize);
			return pm;
		}
		
		@Override
		public List<ArtMadeAudit> getMadeAuditByMadeId(String madeId)throws AppBusinessException {
			Map<String, Object> map= new HashMap<String, Object>();
			map.put("madeId", madeId);
			return artistDao.getMadeAuditByMadeId(map);
		}
		//某一审核单
		@Override
		public ArtMadeAudit getMadeAuditByAuditId(String auditId)throws AppBusinessException {
			Map<String, Object> map= new HashMap<String, Object>();
			map.put("id", auditId);
			return artistDao.getMadeAuditByAuditId(map);
		}
		//通过
		@Transactional(rollbackForClassName = {"com.ywq.common.exception.AppBusinessException;"}, timeout = 10)
		@Override
		public void passMadeAudit(String auditId) throws AppBusinessException {
			//通过
			Map<String, Object> mappass= new HashMap<String, Object>();
			mappass.put("status", "02");
			mappass.put("auditId", auditId);
			artistDao.passMadeAudit(mappass);
			//变更定制made的stage
			ArtMadeAudit artMadeAudit =getMadeAuditByAuditId(auditId);
			String auditKey=artMadeAudit.getAuditKey();
			ArtMade artMade = getArtMadeById(artMadeAudit.getMadeId());
			Map<String, Object> map= new HashMap<String, Object>();
			map.put("identifier", artMade.getIdentifier());
			if(artMade.getStage().equals(auditKey)){
				if("01".equals(auditKey) ){
					//变 made 为 二阶段
					map.put("stage", "02");
					artistDao.updateMadeStage(map);
					
				}else if("02".equals(auditKey)){
					map.put("stage", "03");
					artistDao.updateMadeStage(map);
					//变 made 为 三阶段
				}else if("03".equals(auditKey)){
					map.put("stage", "04");
					artistDao.updateMadeStage(map);
					//变 made 为 结束
				}
			}
			//发送邮件
			CcMailBizImpl send =CcMailBizImpl.getInstance(); 
			//to artistId
			AdminUser adminUser =artistDao.getUserByArtistId(artMadeAudit.getArtistId());
			Member memebr =memberBiz.getMemberById(artMade.getMemberId());
			if(!"".equals(adminUser.getEmail())){
				String to[]={memebr.getEmail()};
				String cs[] = {adminUser.getEmail(),"befamousstore@163.com"};
				String ms[] = null;
				String subject=memebr.getNickname()+"您好，您的定制作品有个最新动态,，请您尽快查看并和艺术家沟通。"; 
				String content="请您登陆http://befamousstore.com，订单号为："+artMade.getIdentifier(); 
				String formEmail="befamousstore@163.com";
				send.send(to,cs,ms,subject,content,formEmail); 
			}
		}
		//根据图片查作者
		@Override
		public ArtArtistPic getArtArtistPicByPicId(String picId)throws AppBusinessException {
			Map<String, Object> map= new HashMap<String, Object>();
			map.put("picId", picId);
			return artistDao.getArtArtistPicByPicId(map);
		}
		//查询作品2个作品
		@Override
		public List<ArtArtistPic> getArtistPicList(String artistId)throws AppBusinessException {
			Map<String, Object> map= new HashMap<String, Object>();
			map.put("artistId",artistId);
			map.put("startId", 0);
			map.put("pageSize", PageSize.PAGE_SIZE_FIND_ARTIST_PIC);
			return artistDao.getArtistPicList(map);
		}
		//
		@Override
		public int getAllPicNumByArtistId(String artistId)throws AppBusinessException {
			Map<String, Object> map= new HashMap<String, Object>();
			map.put("artistId",artistId);
			return artistDao.getAllPicNumByArtistId(map);
		}
		
		@Override
		public void updateMadePay(String madeId,String auditKey) throws AppBusinessException {
			ArtMade artMadeOld = getArtMadeById(madeId);
			if(artMadeOld!=null){
						ArtMade artMade =new ArtMade();
						artMade.setIdentifier(madeId);
						if("01".equals(auditKey) ){
							//首付已付
							artMade.setFirstPay(true);
							artistDao.updateMadePay(artMade);
						}else if("02".equals(auditKey)){
							//中间款已付
							if(artMadeOld.getFirstPay()){
								artMade.setSecondPay(true);
								artistDao.updateMadePay(artMade);
							}
						}else if("03".equals(auditKey)){
							if(artMadeOld.getFirstPay() && artMadeOld.getSecondPay()){
								artMade.setThirdPay(true);
								artistDao.updateMadePay(artMade);
								//尾款已付
							}
						}
					}
			}
		@Override
		public List<ArtArtist> getArtistListForFind()throws AppBusinessException {
			Map<String, Object> map= new HashMap<String, Object>();
			map.put("type",PageSize.ARTIST_FIND);
			List<ArtArtist> artistList=artistDao.getArtistList(map);
			return artistList;
		}
	}
